import { md5 } from 'vux'
import api from '../config/api.js'
import baseUrl from '../baseUrl/index'
import ajax from '../utils/ajax.js'
import moment from 'moment'
import { globalData } from '../config/globalData'

export default class Account {
  static async accessToken (param) {
    let grant_type = 'h5_weal_token'
    let client_id = param.find_type === 0 ? param.phone : param.verify_account
    let timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    let client_secret = param.find_type === 0 ? param.password : param.smsCode
    let shareUrl = param.url
    let sign = ''
    let md = client_secret + timestamp + client_id + grant_type + client_secret
    sign = md5(md).toUpperCase()
    let data = {
      grant_type: grant_type,
      client_id: client_id,
      timestamp: timestamp,
      client_secret: param.find_type === 0 ? client_secret : undefined,
      sign: sign,
      url: shareUrl,
      smsCode: param.find_type === 1 ? param.smsCode : undefined,
      pwd: param.pwd
    }
    if (param.co_number) {
      data.co_number = param.co_number
    }
    let url = baseUrl.server + api.GET_ACCESS_TOKEN
    let res = await ajax.post(url, data)

    return res
  }

  static async login (param = {}) {
    param.pwd = param.password
    param.password = md5(param.password).toLowerCase()
    // eslint-disable-next-line no-return-await
    return await this.accessToken(param)
  }

  static async getCoNumber (phone) {
    let url = baseUrl.server + api.GET_CO_NUMBER
    let data = {
      token: globalData.access_token,
      phone: phone
    }
    let res = await ajax.post(url, data)
    if (res.code == 0) {
      //  ss
    }
  }

  static async ticketLogin (ticket = '') {
    let url = baseUrl.server + api.GET_ACCESS_TOKEN
    let param = {
      grant_type: 'h5_weal_ticket',
      ticket: ticket
    }
    let res = await ajax.post(url, param)
    return res
  }

  static async authLoginGetCode (source = '') {
    let url = baseUrl.server + api.OAUTH_LOGIN_ZGH_GET_CODE
    let param = {
      source: source
    }
    let res = await ajax.post(url, param)
    return res
  }

  static async authLoginGetToken (state = '', code = '') {
    let url = baseUrl.server + api.OAUTH_LOGIN_ZGH_GET_TOKEN
    let param = {
      state: state,
      source_code: code
    }
    let res = await ajax.post(url, param)
    return res
  }

  static async ticketLoginSanxia (param) {
    let url = baseUrl.server + api.GET_ACCESS_TOKEN
    let res = await ajax.post(url, param)
    return res
  }

  static async bind (opt) {
    let data = {
      token: globalData.access_token,
      password: md5(opt.password),
      co_number: opt.co_number,
      phone: opt.phone
    }
    let url = baseUrl.server + api.GET_ACCOUNT_BIND
    let res = await ajax.post(url, data)
    if (res.code == 0) {
      // 00
    }
  }

  static async getJsdkConfig () {
    let url = baseUrl.server + api.GET_JSDK_CONFIG
    let param = {
      token: globalData.access_token,
      url: location.href.split('#')[0]
    }
    let res = await ajax.post(url, param)

    return res
  }

  static async getMobileCode (params = {}) {
    let url = baseUrl.server + api.GET_MOBILE_CODE
    let res = await ajax.post(url, params)
    return res
  }

  static async doRegister (params = {}) {
    params.password = md5(params.password).toLowerCase()
    let url = baseUrl.server + api.DO_REGISTER
    let res = await ajax.post(url, params)
    return res
  }

  static async getVerifyCode (params = {}) {
    let url = baseUrl.server + api.GET_VERIFY_CODE
    let res = await ajax.post(url, params)
    return res
  }

  static async findPassword (params = {}) {
    params.password = md5(params.password).toLowerCase()
    params.repassword = md5(params.repassword).toLowerCase()
    let url = baseUrl.server + api.FIND_PASSWORD
    let res = await ajax.post(url, params)
    return res
  }

  static async resetPassword (params = {}) {
    params.password = md5(params.password).toLowerCase()
    params.repassword = md5(params.repassword).toLowerCase()
    params.token = globalData.access_token
    let url = baseUrl.server + api.RESET_PASSWORD
    let res = await ajax.post(url, params)
    return res
  }

  static async getAgreement (params = {}) {
    let url = baseUrl.server + api.GET_AGREEMENT
    let res = await ajax.get(url, params)
    return res
  }
}
